// pages/personal/myAccount/index/index.js
import api from '../../../../utils/api.js'
const app = getApp()
let apis = api.api;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		active: 0,
		account: 0,
		type: null, //用户的类型
		page: 1, //提现内容
		txType: 0, //切换tab的类型
		txData: {}, //提现数据
		money: '0.00', //用户余额
		userwallet: [], //用户余额信息
		date: [],
		momeyType: null, //显示用户的账户类型
		isempty: false, //数据是否为空
		isBot: false, //判断数据是否加载完
	},
	getMoney() {
		// https://dageda.api.hjdz.ltd/index/withdraw
		wx.navigateTo({
			url: '/pages/personal/myAccount/withdrawal/withdrawal'
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this;
		that.setData({
			type: options.type,
			moneyType: options.msgType

		})
		that.getData(that.data.type, 0)
	},
	onChange(e) {
		this.data.page = 1;
		if (e.detail.name == 0) {
			this.setData({
				txData: {}
			})
			this.getData(this.data.type, 0)
		} else if (e.detail.name == 1) {
			this.setData({
				txData: {}
			})
			this.getData(this.data.type, 1)
		} else {
			this.setData({
				txData: {}
			})
			this.getData(this.data.type, 2)
		}
	},
	getData(type, txType) {
		// https://dageda.api.hjdz.ltd/index/accountLog
		wx.showLoading({
			title: '数据加载中'
		})
		var that = this;
		let url = apis.host + 'index/accountLog';
		let data = {
			user_type: type,
			type: txType + '',
			/* 否	string	类型 默认0 全部 1支出 2收入 */
			page: that.data.page,
			/* 否	string	页码 */
			size: '10',
			format: 'allist',
			/* 是	string	页额 */
			money_type: that.data.moneyType,
			// account  余额   score  积分   commission  佣金
		};
		apis.request(url, data, 'POST').then(function(res) {
			if (res.code == 200) {
				wx.hideLoading()
				for (var i in res.data) {
					if (that.data.txData[i] === undefined) {
						that.data.txData[i] = res.data[i];
					} else {
						that.data.txData[i].price = that.data.txData[i].price * 1 + res.data[i].price *
							1;
						for (var c in res.data[i].data) {
							that.data.txData[i].data.push(res.data[i].data[c])
						}
					}
				}
			}
			let isempty = false,isBot = false;
			if (JSON.stringify(that.data.txData) === '{}') {
				isempty = true
			}
			if (res.data.length == 0) {
				isBot = true
			}
			that.setData({
				txData: that.data.txData,
				isempty: isempty,
				isBot:isBot
			})
		})
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var that = this;
		var type = this.data.type;
		var userInfo = wx.getStorageSync('userAllInfo')
		var withdraw = wx.getStorageSync('userAllInfo').wallet
		if (withdraw.indexOf(type) == -1) {
			var userWallet = [{
				id: null,
				user_id: userInfo.id,
				type: type,
				account: 0.00,
				score: 0,
				commission: 0
			}]
			that.setData({
				userWallet: userWallet,
			})
		} else {
			withdraw.map((item, index) => {
				if (item.type == type) {
					that.data.userWallet.push(item)
				}
			})
			that.setData({
				userWallet: that.data.userWallet,
			})
		}
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		var that = this;
		that.setData({
			page: that.data.page + 1
		})
		that.getData(that.data.type, 0)
	},
})
